package com.linghang.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author qyyzxty@icloud.com
 * @data 2021/4/29
 **/
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream;
        try {
            //构建 SqlSessionFactory 的实例
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //可以将SqlSession理解为PreparedStatement，即可以执行Sql语句的一个玩意
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }

}
